package com.aivin.adovertool.service;

import android.accessibilityservice.AccessibilityService;
import android.content.Intent;
import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityNodeInfo;


import com.aivin.adovertool.util.LyDataTool;
import com.zfdang.touchhelper.tool.LyLog;

import java.util.ArrayList;
import java.util.List;

/**
 * @author ly-dengpw
 */
public class AdOverService extends AccessibilityService {
    private static boolean isAccessibilityServiceReady;
    public static List<String> killAdRecords = new ArrayList<>();

    public static boolean isServiceEnable() {
        return isAccessibilityServiceReady;
    }


    @Override
    public void onInterrupt() {
        isAccessibilityServiceReady = false;
    }

    @Override
    public void onServiceConnected() {
        super.onServiceConnected();
        isAccessibilityServiceReady = true;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return START_STICKY;
    }

    private void handlerSplashAd(AccessibilityEvent event) {
        if (event == null) {
            return;
        }

        AccessibilityNodeInfo currentNodeInfo = getRootInActiveWindow();
        if (currentNodeInfo == null) {
            return;
        }

        LyLog.showLog("handlerSplashAd");
        //d(resourceId="com.byted.pangle:id/tt_splash_skip_btn")
        String viewId = "com.byted.pangle:id/tt_splash_skip_btn";
        List<AccessibilityNodeInfo> skipBtnNodes0 = currentNodeInfo.findAccessibilityNodeInfosByViewId(viewId);
        LyLog.showLog("skipBtnNodes0="+skipBtnNodes0);

        if (skipBtnNodes0 != null && !skipBtnNodes0.isEmpty()) {
            AccessibilityNodeInfo ac = skipBtnNodes0.get(0);
            ac.performAction(AccessibilityNodeInfo.ACTION_CLICK);
            addKillRecord(ac, "skip_viewId");
        }

        List<AccessibilityNodeInfo> skipBtnNodes = currentNodeInfo.findAccessibilityNodeInfosByText("跳过");
        if (skipBtnNodes != null && !skipBtnNodes.isEmpty()) {
            AccessibilityNodeInfo ac = skipBtnNodes.get(0);
            ac.performAction(AccessibilityNodeInfo.ACTION_CLICK);
            addKillRecord(ac, "skip_");
        }


        List<AccessibilityNodeInfo> closeBtnNodes = currentNodeInfo.findAccessibilityNodeInfosByText("关闭");
        if (closeBtnNodes != null && !closeBtnNodes.isEmpty()) {
            AccessibilityNodeInfo ad = closeBtnNodes.get(0);
            ad.performAction(AccessibilityNodeInfo.ACTION_CLICK);
            addKillRecord(ad, "关闭");
        }

    }


    private void addKillRecord(AccessibilityNodeInfo nodeInfo, String tag) {
        String time = LyDataTool.getFormatDateStringYyyyMMddHHmmssFromLongTime(System.currentTimeMillis());
        String msg = nodeInfo.getPackageName() + "  " + time + "  " + tag;
        int maxCount = 10;
        if (killAdRecords.size() > maxCount) {
            killAdRecords.remove(0);
        }
        killAdRecords.add(msg);
    }


    @Override
    public void onAccessibilityEvent(AccessibilityEvent event) {
        handlerSplashAd(event);
    }
}
